package cn.shuibo.enums;

import cn.hutool.core.util.DesensitizedUtil;

import java.util.function.Function;

/**
 * 脱敏策略，枚举类，针对不同的数据定制特定的策略
 *
 * @Author guoyb
 * @Date 2022/6/28  16:15
 */
public enum SensitiveStrategy {

    /**
     * 用户名
     */
    USERNAME(s -> s.replaceAll("(\\S)\\S(\\S*)", "$1*$2")),
    /**
     * 身份证
     */
    ID_CARD(s -> s.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1****$2")),
    /**
     * 手机号
     */
    PHONE(s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")),
    /**
     * 地址
     */
    ADDRESS(s -> s.replaceAll("(\\S{3})\\S{2}(\\S*)\\S{2}", "$1****$2****")),


    //用户id
    USER_ID(s -> String.valueOf(DesensitizedUtil.userId())),

    //中文名
    CHINESE_NAME(s -> DesensitizedUtil.chineseName(String.valueOf(s))),

    //身份证号
    ID_CARD1(s ->DesensitizedUtil.idCardNum(String.valueOf(s), 1, 2)),

    //手机号
    MOBILE_PHONE(s -> DesensitizedUtil.mobilePhone(String.valueOf(s))),

    //地址
    ADDRESS1(s ->DesensitizedUtil.address(String.valueOf(s), 8)),

    //电子邮件
    EMAIL(s -> DesensitizedUtil.email(String.valueOf(s))),

    //密码
    PASSWORD(s ->DesensitizedUtil.password(String.valueOf(s))),
    //中国大陆车牌，包含普通车辆、新能源车辆
    CAR_LICENSE(s -> DesensitizedUtil.carLicense(String.valueOf(s))),
    //银行卡
    BANK_CARD(s -> DesensitizedUtil.bankCard(String.valueOf(s))),
    ;


    private final Function<String, String> desensitizer;

    SensitiveStrategy(Function<String, String> desensitizer) {
        this.desensitizer = desensitizer;
    }

    public Function<String, String> desensitizer() {
        return desensitizer;
    }
}
